package com.hlkj.warboot.module_sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.hlkj.warboot.configurer.util.base.BaseEntityUtil;
import com.hlkj.warboot.module_sys.entity.MailEntity;
import com.hlkj.warboot.module_sys.service.AsynService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

/**
 * @Description: TODO 全局异步任务控制器
 * @Author: lixing
 * @Date: 2020/6/12 7:58
 */
@RestController
@RequestMapping("asyn")
public class AsynController {
    @Resource
    private AsynService asynService;

    /*
     * @Description TODO 发送邮件
     * @Author lixing
     * @Date 2019/2/16 13:08
     * @Param [entity]
     * @return com.alibaba.fastjson.JSONObject
     */
    @PostMapping("sendMail")
    public JSONObject sendMail(MailEntity entity) throws Exception {
        CompletableFuture<JSONObject> sendMailCompletableFuture = asynService.sendHtmlMail( entity );
        CompletableFuture.allOf(sendMailCompletableFuture).join();
        return sendMailCompletableFuture.get();
    }

    /*
     * @Description TODO 上传文件
     * @Author lixing
     * @Date 2019/3/30 15:51
     * @Param [baseEntityUtil]
     * @return com.alibaba.fastjson.JSONObject
     */
    @PostMapping("uploadFile")
    public JSONObject uploadFile(BaseEntityUtil baseEntityUtil) throws Exception {
        CompletableFuture<JSONObject> uploadImageCompletableFuture = asynService.uploadFile(baseEntityUtil);
        CompletableFuture.allOf(uploadImageCompletableFuture).join();
        return uploadImageCompletableFuture.get();
    }

    /*
     * @Description TODO 通过上传Excel来导入数据
     * @Author lixing
     * @Date 2019/3/30 15:51
     * @Param [multipartFiles, baseEntityUtil] 
     * @return com.alibaba.fastjson.JSONObject
     */
    @PostMapping("importDataByExcel")
    public JSONObject uploadExcel(@RequestParam("file") MultipartFile[] multipartFiles, BaseEntityUtil baseEntityUtil) throws Exception {
        CompletableFuture<JSONObject> uploadExcelCompletableFuture = asynService.importDataByExcel( multipartFiles, baseEntityUtil );
        CompletableFuture.allOf(uploadExcelCompletableFuture).join();
        return uploadExcelCompletableFuture.get();
    }

    /*
     * @Description TODO 通过上传Excel来导入数据（新）
     * @Author lixing
     * @Date 2019/3/30 15:51
     * @Param [multipartFiles, baseEntityUtil]
     * @return com.alibaba.fastjson.JSONObject
     */
    @PostMapping("importDataByExcelNew")
    public JSONObject uploadExcelNew(@RequestParam("file") MultipartFile[] multipartFiles, BaseEntityUtil baseEntityUtil) throws Exception {
        CompletableFuture<JSONObject> uploadExcelCompletableFuture = asynService.importDataByExcelNew( multipartFiles, baseEntityUtil );
        CompletableFuture.allOf(uploadExcelCompletableFuture).join();
        return uploadExcelCompletableFuture.get();
    }

}
